import promisePool from './Pool.js'

export default class PermissionDao {

    static async insert (permission) {
        let connection = null
        try {
            connection = await promisePool.getConnection()
        
            let result = await connection.query('insert into `permission` (`name`, `method`) values (?, ?)', [permission.name, permission.method])

            return result[0].affectedRows
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async delete (permission) {

        let connection = null
        try {
            connection = await promisePool.getConnection()

            //开启事务
            await connection.beginTransaction()
            
            await connection.query('delete from `role_permission` where `permission_id` = ?', permission.id)

            let result = await connection.query('delete from `permission` where `id` = ?', permission.id)

            //提交事务
            await connection.commit()

            return result[0].affectedRows
        } catch (error) {
            //事务回滚
            await connection.rollback()
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async update (permission) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            //获取对象中属性值不为null的键,值对
            let keys = []
            let key_val = []
            for (const key of Reflect.ownKeys(permission)) {
                if (permission[key] !== null && key !== 'id') {
                    keys.push(key)
                    key_val.push(key)
                    key_val.push(permission[key])
                }
            }

            //遍历拼接sql
            let sql = 'update `permission` set '
            let sql_sets = ''

            for (let i = 0; i < keys.length; i++) {
                i == keys.length - 1 ? sql_sets += '?? = ?' : sql_sets += '?? = ?,'
            }

            sql += sql_sets
            sql += ' where id = ?'
        
            let result = await connection.query(sql, [...key_val, permission.id])

            return result[0].affectedRows
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectById (id) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let result = await connection.query('select `id`, `name`, `method` from `permission` where `id` = ?', id)

            return result[0]
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectByLimit (start, end) {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let result = await connection.query('select `id`, `name`, `method` from `permission` limit ?, ' + end, start)

            return result[0]
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }

    static async selectCount () {
        let connection = null
        try {
            connection = await promisePool.getConnection()

            let result = await connection.query('select count(id) as total from `permission`')

            return result[0][0]
        } catch (error) {
            return error
        } finally {
            if (connection) connection.release()
        }
    }
}